﻿using OF.Utility;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Msg.Api.Test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.textBox1.Text = "http://api.banmachewu.com/msg/api";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string a = "";
            SendMsgPost("测试", "13521038429", 1, "test", 1, 0, "", out a);
        }

        public bool SendMsgPost(string smsContent, string strMobile, int platformId, string typeName, int channelId, int opUserId, string opUserName, out string msg)
        {
            try
            {
                string targetUrl = string.Format("{0}/sms/sendshortmessage", this.textBox1.Text);
                string param = string.Format("Content={0}&PlatformId={1}&SendTypeName={2}&ChannelId={3}&OpUserId={4}&OpUserName={5}&Mobile={6}", smsContent, platformId, typeName, channelId, opUserId, opUserName, strMobile);

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(targetUrl);

                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.Accept = "*/*";
                request.Headers.Add("Accept-Encoding: gzip,deflate");
                request.Headers.Add("Accept-Language: zh-cn");
                request.Headers.Add("Authorization", MakeAuthorationHeaderValue(targetUrl));

                byte[] bytes = Encoding.UTF8.GetBytes(param);
                Stream requestStream = request.GetRequestStream();
                requestStream.Write(bytes, 0, bytes.Length);
                requestStream.Close();
                var response = request.GetResponse();
                using (var reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")))
                {
                    string resultStr = reader.ReadToEnd();
                    msg = resultStr;
                  //  ResultData rData = JsonHelper.JsonDeserialize<ResultData>(resultStr);
               //     if (rData == null) return false;
                    //if (rData.message.Length <= 15)
                    //{
                    //    //发送失败 log
                    //    return false;
                    //}
                    return true;
                }
            }
            catch (Exception e)
            {
                msg = "发送短信失败，接口连接失败，请联系管理员";
                return false;
            }
        }

        /// <summary>
        /// 生成token
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        static string MakeAuthorationHeaderValue(string url)
        {
            string szReturn = string.Empty;
            var scheme = "BMCW ";
            string key = "cgs";

            int ms = DateTime.Now.Millisecond;
            if (ms < 100)
            {
                ms += 100;
            }
            string token;
            if (url.Length > 32)
            {
                token = key + url.Substring(8, 24) + ms;
            }
            else
            {
                token = key + url.Substring(9) + ms;
            }
            szReturn = ms + MD5Helper.MD5String(token);
            return scheme + szReturn;
        }
    }
}
